package com.jghz.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

@Service
public class LogService implements InitializingBean {

	private Map<String, String> paths = new HashMap<>();

	private Map<String, Object> log4j2Map = null;

	public Map<String, Object> getPageData(int pageIndex, int pageSize) {

		Map<String, Object> returnMap = new HashMap<>();

		List<File> sources = getLogFiles(paths.get("transfer.log.path"));

		int recordsCount = sources.size();
		int totalPage = (recordsCount - 1) / pageSize + 1;

		List<File> pageList = null;
		if (pageIndex == totalPage) {
			pageList = sources.subList((pageIndex - 1) * pageSize, recordsCount);
		} else {
			pageList = sources.subList((pageIndex - 1) * pageSize, pageIndex * pageSize);
		}
		returnMap.put("total", recordsCount);
		returnMap.put("rows", processPageDate(pageList));
		return returnMap;
	}

	public String getPath(String key) {
		return paths.get(key);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		YamlMapFactoryBean yaml = new YamlMapFactoryBean();
		yaml.setResources(new ClassPathResource("log4j2.yml"));
		log4j2Map = yaml.getObject();
		this.getLogPathMap();
	}

	private List<File> getLogFiles(String logPath) {
		File file = new File(logPath);
		File[] fs = file.listFiles();
		Arrays.sort(fs, new Comparator<File>() {
			@Override
			public int compare(File f1, File f2) {
				long diff = f1.lastModified() - f2.lastModified();
				if (diff > 0)
					return -1;
				else if (diff == 0)
					return 0;
				else
					return 1;
			}
		});
		List<File> list = Arrays.asList(fs);
		return list;
	}

	private List<Map<String, Object>> processPageDate(List<File> files) {
		List<Map<String, Object>> finalList = new ArrayList<>();
		for (File item : files) {
			item.getAbsolutePath();
			Map<String, Object> map = new HashMap<>();
			map.put("fileName", item.getName());
			map.put("size", FileUtils.byteCountToDisplaySize(item.length()));
			DateTime dateTime = new DateTime(item.lastModified());
			map.put("lastModifiedTime", dateTime.toString("yyyy-MM-dd HH:mm:ss"));
			finalList.add(map);
		}
		return finalList;
	}

	@SuppressWarnings("unchecked")
	private void getLogPathMap() {
		Map<String, Object> conf = (Map<String, Object>) log4j2Map.get("Configuration");
		Map<String, Object> prop = (Map<String, Object>) conf.get("Properties");
		List<Map<String, Object>> props = (List<Map<String, Object>>) prop.get("Property");

		for (Map<String, Object> map : props) {
			String name = ObjectUtils.toString(map.get("name"));
			String value = ObjectUtils.toString(map.get("value"));
			if (StringUtils.contains(name, "path")) {
				paths.put(name, value);
			}
		}

		for (Entry<String, String> entry : paths.entrySet()) {
			for (Map<String, Object> map : props) {
				String name = ObjectUtils.toString(map.get("name"));
				if (StringUtils.contains(entry.getValue(), name)) {
					entry.setValue(StringUtils.replace(entry.getValue(), "${" + name + "}",
							ObjectUtils.toString(map.get("value"))));
				}
			}
		}
	}
}
